package com.spring.mvc.http.test;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 * 两种最流行非对称加密算法 RSA DSA
 * @author Administrator
 *
 */
public class NotSymmetryDemo {
	
	public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	//用于生成公钥和私钥对，基于RSA算法生成对象
	private KeyPairGenerator keyPairGen;
	//保存密钥对
	private KeyPair keyPair; 
	
	public NotSymmetryDemo() {
		try {
			keyPairGen=KeyPairGenerator.getInstance("RSA");
			//初始化密钥对生成器，密钥大小为1024位
			keyPairGen.initialize(1024);
			//生成一个密钥对，保存在keyPair中
			keyPair=keyPairGen.generateKeyPair();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取公钥
	 * @return
	 */
	public RSAPublicKey getPublicKey(){
		return (RSAPublicKey) keyPair.getPublic();
	}
	
	/**
	 * 获取私钥
	 * @return
	 */
	public RSAPrivateKey getPriverKey(){
		return (RSAPrivateKey) keyPair.getPrivate();
	}
	
	/**
	 * 加密
	 * @param publicKey
	 * @param srcBytes
	 * @return
	 */
	public byte[] encrypt(RSAPublicKey publicKey , byte[] srcBytes){
		if(publicKey!=null){
			try{
				//Cipher负责完成加密解密工作
				Cipher cipher=Cipher.getInstance("RSA");
				cipher.init(Cipher.ENCRYPT_MODE, publicKey);
				//加密，结果保存进resultBytes
				byte[] resultBytes=cipher.doFinal(srcBytes);
				return resultBytes;
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 解密
	 * @param priverKey
	 * @param encBytes
	 * @return
	 */
	public byte[] decrypt(RSAPrivateKey priverKey,byte[] encBytes){
		if(priverKey!=null){
			try {
				Cipher cipher=Cipher.getInstance("RSA");
				cipher.init(Cipher.DECRYPT_MODE, priverKey);
				byte[] resultBytes=cipher.doFinal(encBytes);
				return resultBytes;
			} catch (InvalidKeyException e) {
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} catch (NoSuchPaddingException e) {
				e.printStackTrace();
			} catch (IllegalBlockSizeException e) {
				e.printStackTrace();
			} catch (BadPaddingException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		NotSymmetryDemo demo=new NotSymmetryDemo();
		String str="hello world!";
		//公钥加密
		System.out.println(demo.getPublicKey());
		byte[] s=demo.encrypt(demo.getPublicKey(), str.getBytes());
		StringBuilder sb=new StringBuilder();
		for (int i = 0; i < s.length; i++) {
			System.out.print(s[i]+" ");
		}
		System.out.println();
		for (int i = 0; i < s.length; i++) {
			sb.append(hexChar[(s[i]&0xf0)>>4]);
			sb.append(hexChar[s[i]&0x0f]+" ");
		}
		System.out.println(sb.toString().toUpperCase());
		System.out.println(demo.getPriverKey());
		/*//私钥解密
		s=demo.decrypt(demo.getPriverKey(), s);
		System.out.println(new String(s));*/
		
		//1001<<4
		//10010000
		//00000110
		//10010110
	}
	//01101010
	//10010101
	//10010110
}
